package handler;

import entity.*;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CommentListHandler implements ResultSetHandler<List<Comment>> {
    @Override
    public List<Comment> handle(ResultSet resultSet) throws SQLException {
        List<Comment> commentList = new ArrayList<>();
        while (resultSet.next()) {
            Comment comment = new Comment();
            comment.setId(resultSet.getInt("commentId"));
            comment.setContent(resultSet.getString("content"));
            comment.setCommentTime(resultSet.getDate("commentTime"));

            User user = new User();
            user.setId(resultSet.getInt("userId")); // 使用别名
            user.setUserName(resultSet.getString("userName")); // 使用别名
            comment.setUser(user);

            Pet pet = new Pet();
            pet.setId(resultSet.getInt("petId")); // 使用别名
            pet.setPetName(resultSet.getString("petName")); // 使用别名
            comment.setPet(pet);


            commentList.add(comment);
        }
        return commentList;
    }
}
